<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/18
 * Time: 14:18
 * @copyright: ©2024 永州微聚 商网络科技有限公司
 * @link: https://www.weijuYunke.com
 */
namespace app\admin\controller;
use app\merchant\model\GoodsClassify;
use app\merchant\model\GoodsDiscount;
use app\merchant\model\GoodsSku;
use app\Request;
use app\merchant\model\Goods as GoodsModel ;
use think\Exception;

class Goods
{

    /**
     添加商品主体
     */
    public function addGoods(Request $request){
        $param = $request->param();
        if (!isset($param['name']) || empty($param['name'])){
            return error('请入商品名称');
        }
        if (!isset($param['classify_id']) || empty($param['classify_id'])){
            return error('请选择商品分类');
        }
        try{
            $data =[
                'name' => $param['name'],
                'host_img' => $param['host_img'],
                'desc' => $param['desc'],
               'title' => $param['title'],
                'delivery_time' => $param['delivery_time'],
                'price' => $param['price'],
                'market_price' => $param['market_price'],
                'classify_id' => $param['classify_id'],
                'status' => 1,
                'create_time' => time(),
                //'store_id' => $request->staff_account['store_id'],
                'update_time' => time(),
                'buying_pattern' => $param['buying_pattern'],
                'inventory' => $param['inventory'],
                'is_swich_level' => $param['is_swich_level']
            ];
            $goodsModel = new  GoodsModel();
            $goodsModel->insert($data);
            $id = $goodsModel->getLastInsID();
            $dataDiscount  =[];
            $discount = $param['discount'];
            foreach ($discount as $key => $value){
                $dataDiscount[$key]['level_id'] = $value['level_id'];
                $dataDiscount[$key]['discount'] = $value['discount'];
                $dataDiscount[$key]['goods_id'] = $id;
                $dataDiscount[$key]['create_time'] = time();
            }
            $goodsDiscountModel = new GoodsDiscount();
            $goodsDiscountModel->insertAll($dataDiscount);
        }catch (Exception $exception){
            return error($exception->getMessage());
        }
        return adminSuccess('添加成功');
    }



    public function editGoods(Request $request){
        $param = $request->param();
        if (!isset($param['name']) || empty($param['name'])){
            return error('请入商品名称');
        }
        if (!isset($param['id']) || empty($param['id'])){
            return error('请选择商品');
        }
       try{
            $data =[
                'name' => $param['name'],
                'host_img' => $param['host_img'],
                'desc' => $param['desc'],
                'title' => $param['title'],
                'delivery_time' => $param['delivery_time'],
                'price' => $param['price'],
                'market_price' => $param['market_price'],
                'classify_id' => $param['classify_id'],
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
                'buying_pattern' => $param['buying_pattern'],
                'inventory' => $param['inventory'],
                'is_swich_level' => $param['is_swich_level']
            ];
            $goodsModel = new  GoodsModel();
            $goodsModel->where(['id' => $param['id']])->update($data);
            $discount = $param['discount'];
           // var_dump($discount);exit;
            $goodsDiscountModel = new GoodsDiscount();
            $goodsDiscountModel->where(['goods_id'=>$param['id']] )->delete();
            foreach ($discount as $key => $value){
                    $goodsDiscountModel->insert([
                        'level_id' => $value['level_id'],
                        'goods_id'=>$param['id'],
                        'discount' =>$value['discount'],
                        'create_time' => time()
                    ] );

            }
       }catch (Exception $exception){
            return error($exception->getMessage());
        }
        return adminSuccess('修改成功');
    }



    public  function goodsList(Request $request){
        $param =  $request ->param();
        $limit  =  10;
        $start = 0;
        if (isset($param['limit']) &&  !empty($param['limit'])){
            $limit = $param['limit']  ;
        }
        if (isset($param['page']) &&  !empty($param['page'])){
            $start = ($param['page'] -1)  *  $limit;
        }
        $where = [];
        //$where[] =  ['store_id','=',$request->staff_account['store_id']];
        $goodsModel = new GoodsModel();
        $where[] =  ['status','in',[1,2]];
        if (isset($param['status']) && !empty($param['status'])){
            $where[] =  ['status','=',$param['status']];
        }
        if (isset($param['name']) && !empty($param['name'])){
            $where[] =  ['name','like','%'.$param['name'].'%'];
        }
        $goodsList =  $goodsModel -> where($where)->order('create_time','desc')->limit($start,$limit)->select()  ->toArray();
        $count =  $goodsModel -> where($where)->count();
        return adminSuccess('商品列表',['list' => $goodsList,'count' => $count]);
    }

    public  function  upGoodsStatus(Request $request){
        $param = $request->param();
        $goodsModel = new GoodsModel();
        $ids = explode(',',$param['id']);
        foreach ($ids as $value){
            $goodsModel->where(['id'=>$value])->update(['status' => $param['status']]);
        }

        return adminSuccess('操作成功');
    }


    public  function goodsInfo(Request $request){
        $param = $request->param();
        $goodsModel = new GoodsModel();
        $goodInfo = $goodsModel->where(['id'=>$param['id']])->find();
        $goodsDiscountModel = new GoodsDiscount();
        $goodsDiscountData = $goodsDiscountModel->where(['goods_id' => $param['id']])->select()->toArray();
        $goodInfo['discount'] = $goodsDiscountData;
        return adminSuccess('成功',$goodInfo);
    }


    public function gooodsDiscount(Request $request){
        $param =$request->param();
        $goodsDiscountModel = new \app\home\model\GoodsDiscount();
        $discount = 0;
        $userModel = new \app\home\model\User();
        $userInfo = $userModel->where(['id' => $param['uid']])->find();
        $goodsDiscountInfo = $goodsDiscountModel->goodsLevelDiscount($userInfo['level_id'], $param['goods_id']);
        if (!empty($goodsDiscountInfo) && $goodsDiscountInfo['discount'] > 0) {  // 针对商品设置了 折扣
            $discount  = $goodsDiscountInfo['discount'];
        } else {
            $levelModel = new \app\merchant\model\Level();
            $levelInfo = $levelModel->where(['id' => $userInfo['level_id']])->find();
            if ($levelInfo['shopping_preferential'] > 0) { // 针对商城全局的折扣
                $discount = $levelInfo['shopping_preferential'];
            } else {
                if ($levelInfo['preferential'] > 0) {  // 全局的折扣
                    $discount = $levelInfo['preferential'];
                }
            }
        }
        return adminSuccess('商品详情', ['goods_id' => $param['goods_id'],'discount' => $discount]);
    }

    public function consumptionDiscount(Request $request){
        $param =$request->param();
        $discount = 0;
        $userModel = new \app\home\model\User();
        $userInfo = $userModel->where(['id' => $param['uid']])->find();
        $levelModel = new \app\merchant\model\Level();
        $levelInfo = $levelModel->where(['id' => $userInfo['level_id']])->find();
        if ($levelInfo['shopping_preferential'] > 0) { // 针对商城全局的折扣
            $discount = $levelInfo['shopping_preferential'];
        } else {
            if ($levelInfo['preferential'] > 0) {  // 全局的折扣
                $discount = $levelInfo['preferential'];
            }
        }
        return adminSuccess('消费折扣', ['discount' => $discount]);
    }

    /**
     * 添加商品规格
     */
    /* public  function  addGoodsSku(Request $request){
         $param = $request->param();

         foreach ($param['colour'] as $key => $value){
             $data[] =[
                 'goods_id' => $param['goods_id'],
                 'colour' =>$value,
                 'unit' => $param['unit'][$key],
                 'inventory' => $param['inventory'][$key],
                 'price' => $param['price'][$key],
                 'market_price' => $param['market_price'][$key],
                 'type' => $param['type'][$key],
                 'integral' => $param['integral'][$key],
                 'img' => $param['img'][$key],
                 'status' => 1,
                 'create_time' => time(),
             ];
         }
         $goodsSkuModel = new GoodsSku();
         $goodsSkuModel->insertAll($data);
         return adminSuccess('添加成功');
     }*/

    /* public function editGoodsSku(Request $request){
         $param = $request->param();
         $goodsSkuModel = new GoodsSku();
         foreach ($param['colour'] as $key => $value){
             $data =[
                 'goods_id' => $param['goods_id'],
                 'colour' =>$value,
                 'unit' => $param['unit'][$key],
                 'inventory' => $param['inventory'][$key],
                 'price' => $param['price'][$key],
                 'market_price' => $param['market_price'][$key],
                 'type' => $param['type'][$key],
                 'integral' => $param['integral'][$key],
                 'img' => $param['img'][$key],
                 'status' => 1,
                 'create_time' => time(),
             ];
             $goodsSkuModel->where(['id' => $key])->update($data);
         }
         return adminSuccess('修改成功');
     }*/

    /*   public  function goodsSkuList(Request $request){
        $param =  $request ->param();
        $limit  =  10;
        $start = 0;
        if (isset($param['limit']) &&  !empty($param['limit'])){
            $limit = $param['limit']  ;
        }
        if (isset($param['page']) &&  !empty($param['page'])){
            $start = ($param['page'] -1)  *  $limit;
        }
        $GoodsSkuModel = new GoodsSku();
        $where['goods_id'] =  $param['goods_id'];

        $goodsSku =  $GoodsSkuModel -> where($where)->limit($start,$limit)->select()->toArray();

        return adminSuccess('商品列表',$goodsSku);
    }*/
}